package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.CommonListDataResponse;
import com.redwine.app.net.responses.DownOptionsBean;
import com.redwine.app.net.responses.MultipartRequest;
import com.redwine.app.net.responses.RedwineDetailResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.BitmapUtils;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 我要报价界面
 * 作者：gassion on 17/8/7 22:46
 * 最可怕的敌人，就是没有坚强的信念。
 */

public class OfferActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.offer_iv_upload_img)
    private ImageView offerIvUploadImg;
    @ViewInject(R.id.offer_et_message)
    private EditText offerEtMessage;
    @ViewInject(R.id.offer_et_price)
    private EditText offerEtPrice;
    @ViewInject(R.id.offer_et_send_city)
    private EditText offerEtCity;
    @ViewInject(R.id.offer_tv_send_city)
    private TextView offerTvSendCity;
    public static final String PUBLISH_ENTITY = "publishEntity";
    private RedwineDetailResponse.DataEntity.PublishEntity publishEntity;
    private List<String> listPics = new ArrayList<>();//上传图片的集合
    private List<DownOptionsBean> mCity = new ArrayList<>();//产地
    private String mCityID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer);
        ViewUtils.inject(this);
        initView();
        //requestProvinceData();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            publishEntity = (RedwineDetailResponse.DataEntity.PublishEntity) intent.getSerializableExtra(PUBLISH_ENTITY);
        }
        headerTitle.setText(R.string.i_want_offer);
        layoutBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.layout_back, R.id.offer_tv_confirm, R.id.offer_iv_upload_img, R.id.offer_tv_send_city})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.offer_iv_upload_img:
                choosePhotoDialog();
                break;

            case R.id.offer_tv_confirm:
                requestData();
                break;

            case R.id.offer_tv_send_city:

                break;
        }
    }

    @Override
    protected void photoCallBack(String path) {
        listPics.add(path);
        BitmapUtils.loadImage(offerIvUploadImg, path, BitmapUtils.BANNER);
    }

    /**
     * 提交校验
     */
    private void requestData() {

        if (listPics.isEmpty()) {
            Utils.showToast("采购图片不能为空！");
            return;
        }

        final List<File> files = new ArrayList<>();
        for (int i = 0; i < listPics.size(); i++) {
            files.add(new File(listPics.get(i)));
        }

        String price = offerEtPrice.getText().toString().trim();
        if (TextUtils.isEmpty(price)) {
            Toast.makeText(this, "填写报价 元/瓶", Toast.LENGTH_SHORT).show();
            return;
        } else {
            int markVal;
            try {
                markVal = Integer.valueOf(price);
            } catch (NumberFormatException e) {
                markVal = 0;
            }
            if (markVal <= 0) {
                Utils.showToast("报价输入有误！");
                return;
            }
        }

        String message = offerEtMessage.getText().toString().trim();
        if (TextUtils.isEmpty(message)) {
            Toast.makeText(this, "留言", Toast.LENGTH_SHORT).show();
            return;
        }

        String city = offerEtCity.getText().toString().trim();
        if (TextUtils.isEmpty(city)) {
            Toast.makeText(this, "填写发货城市", Toast.LENGTH_SHORT).show();
            return;
        }

        showWaiting();
        String url = NetworkConstants.REDWINE_QUOTE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("price", price);
        params.put("city", city);
        params.put("msg", message);
        params.put("uid", Global.user.getUid());
        params.put("publish_id", publishEntity.getId());
        MultipartRequest<CommonListDataResponse> multipartRequest = new MultipartRequest<>(url, CommonListDataResponse.class, "images", files, params,
                new Response.Listener<CommonListDataResponse>() {
                    @Override
                    public void onResponse(CommonListDataResponse response) {
                        dismissWaiting();
                        Utils.showToast(response.getMsg());
                        if (response.isSuccess()) {
                            EventBus.getDefault().post(new SendMsgEvent(SendMsgEvent.REFURBISH_PROCUREMENT_DETAIL_DATA));
                            finish();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
            }
        });
        multipartRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 60 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        APP.getInstance().getRequestQueue().add(multipartRequest);
    }
}
